<?php
/* ===========================
 * * 数据库类
 * @ by 子不语 爱不语CMS
 * @ QQ:76992327
 * @ Create:2013.04.6
  ============================ */
if (!defined('KKINC')) exit('Request Error!');
class user
{
    public $username;    //用户名
    private $userpass;    //密码
    public $userid;    //用户id
    public $userlevel;    //用户级别
    public $nickname;
    private $authtable = 'user';    //验证用数据表
    private $usecookie = true;    //使用cookie保存sessionid
    private $cookiepath = '/';    //cookie路径
    private $cookietime = 108000;    //cookie有效时间
    private $err_username = '用户名或密码不对';    //用户名无效提示
    private $err_user = '用户名或密码不对';            //用户无效提示(被封禁)
    private $err_password = '用户名或密码不对';        //密码错误提示
    public $err;    //出错提示
    private $errorreport = false;    //显示错误

    function isLoggedin()    //判断是否登录
    {

        if (isset($_COOKIE['sid']))    //如果cookie中保存有sid
        {
            session_id($_COOKIE['sid']);
            session_start();
            if(empty($_SESSION['userid']))return false;
            //var_dump($_SESSION);
            $this->username = $_SESSION['username'];
            $this->nickname = $_SESSION['nickname'];
            $this->userid = $_SESSION['userid'];
            $this->userlevel = $_SESSION['userlevel'];
            return true;
        } else    //如果cookie中未保存sid,则直接检查session
        {
            session_start();
            if (isset($_SESSION['username'])){
                $this->username = $_SESSION['username'];
                $this->nickname = $_SESSION['nickname'];
                $this->userid = $_SESSION['userid'];
                $this->userlevel = $_SESSION['userlevel'];
                return true;
            }
        }
        return false;
    }

    function userAuth($username, $userpass)    //用户认证
    {
        $this->username = $username;
        $this->userpass = $userpass;
        $sql = 'select uid,username,nickname,password,groupid,status from __user where `username`=\'' . $this->username . '\'';

        $row = db::get_one($sql);
        if ($row->username)    //找到此用户
        {
            if ($row->status == 0)    //此用户被封禁
            {
                //$this->errReport($this->err_user);
                $this->err = $this->err_user;
                $this->log(array('status' => 0, 'userid' => $row->uid));
                return false;
            } elseif ($this->enPass($userpass) == $row->password)    //密码匹配
            {
                $this->userid = $row->uid;
                $this->userlevel = $row->groupid;
                $this->nickname = $row->nickname;
                $this->log(array('status' => 1, 'userid' => $row->uid));
                $this->setSession();
                return true;
            } else    //密码不匹配
            {
                $this->err = $this->err_password;
                $this->log(array('status' => 0, 'userid' => $row->uid));
                return false;
            }
        } else    //没有找到此用户
        {
            //$this->errReport($this->err_username);
            $this->log(array('status' => 0));
            $this->err = $this->err_username;
            return false;
        }
    }

    function log($data)
    {

        $data['username'] = $this->username;
        $data['password'] = $this->userpass;
        $data['nickname'] = $this->nickname;
        $data['logintime'] = time();
        $data['ip'] = get_ip();
        db::_insert('user_log', $data);
    }

    function setSession()    //置session
    {
        $sid = uniqid('sid');    //生成sid
        //echo 'this ->'.$sid;
        session_id($sid);
        session_start();
        $_SESSION['username'] = $this->username;    //给session变量赋值
        $_SESSION['userid'] = $this->userid;    //..
        $_SESSION['userlevel'] = $this->userlevel;    //..
        $_SESSION['nickname'] = $this->nickname;    //..
        //var_dump($_SESSION);
        if ($this->usecookie)    //如果使用cookie保存sid
        {
            if (!setcookie('sid', $sid, time() + $this->cookietime, $this->cookiepath))
               echo 'set cookie failed';// $this->errReport('set cookie failed');
        } else
            setcookie('sid', time() - 3600);    //清除cookie中的sid
    }

    function userLogout()    //用户注销
    {
        session_start();
        unset($_SESSION['username']);    //清除session中的username
        if (setcookie('sid', time() - 3600))
            //清除cookie中的sid
            return true;
        else
            return false;
    }

    //判断是否存在此用户
    function get_username($user)
    {
        $res = db::get_one('select uid from __user where username=\'' . $user . '\' limit 1');
        if ($res->uid) return true;
        return false;
    }
    //判断是否存在此用户 微信直接登录用
    function wx_has_username($user){
        $row = db::get_one('select uid,username,nickname,groupid from __user where username=\'' . $user . '\' limit 1');
        if ($row->username){
            $this->userid = $row->uid;
            $this->userlevel = $row->groupid;
            $this->username = $user;
            $this->nickname = $row->nickname;
            $this->log(array('status' => 1, 'userid' => $row->uid));
            $this->setSession();
            return true;
        }
        return false;
    }
    function wx_has_logo($user,$logo=''){
        $row = db::get_one('select logo from __user where username=\'' . $user . '\' limit 1');
        if ($row->logo){
            return true;
        }
        db::_query('update from __user set logo=\''.$logo.'\' where username=\''.$user.'\'');
        return false;
    }
    function has_email($email){
        $res = db::get_one('select uid from __user where email=\'' . $email . '\' limit 1');
        if ($res->uid) return true;
        return false;
    }
    function reg($arr)
    {
        if ($arr['email'] && $arr['pass']) {
            if($arr['wx']){
                if ($this->get_username($arr['pass'])) return '3';
                if ($this->has_email($arr['email'])) return '2';
            }else {
                if ($this->get_username($arr['email'])) return '2';
            }
            $group = db::get_one('select id from __user_group order by id asc limit 1');

            $data=array('in_time' => time(), 'groupid' => $group->id, 'mobile' => $arr['mobile'], 'nickname' => $arr['nick'], 'username' => $arr['email'], 'password' => $this->enPass($arr['pass']), 'email' => $arr['email'],'reg_type'=>$arr['wx']);
            if($arr['wx'])$data['username']=$arr['pass'];
            db::_insert('user', $data);
            $lastid = db::last_id();
            if ($lastid) {
                unset($arr['email']);
                unset($arr['pass']);
                unset($arr['nick']);
                unset($arr['mobile']);
                unset($arr['wx']);
                unset($arr['logo']);
                unset($arr['reg_type']);
                if (count($arr['cate']) > 0) {
                    foreach ($arr['cate'] as $k => $v) {
                        if ($v)
                            db::_insert('user_type', array('userid' => $lastid, 'categoryid' => $k));
                    }
                }
                unset($arr['cate']);
                $arr['uid'] = $lastid;
                db::_insert('user_street', $arr);
                return 1;
            }
        }
        return 0;
    }

    function update($arr)
    {

        if(strlen($arr['pass'])>=6 && strlen($arr['pass']<=20)){
            $data['password']=$this->enPass($arr['pass']);
        }
        if($arr['email']){
            $data['email']=$arr['email'];
        }
        if($arr['nick']){
            $data['nickname']=$arr['nick'];
        }
        if($arr['mobile']){
            $data['mobile']=$arr['mobile'];
        }
        if(db::_update('user',$data,'uid='.$this->userid)){
            $data=array();
            if($arr['address']){
                $data['address']=$arr['address'];
            }
            if($arr['s1']){
                $data['s1']=$arr['s1'];
            }
            if($arr['s2']){
                $data['s2']=$arr['s2'];
            }
            if($arr['family']){
                $data['family']=$arr['family'];
            }
            db::_update('user_street',$data,'uid='.$this->userid);
            return 1;
        }
        return 2;
    }
    function enPass($pass)
    {
        return md5(M_KEY . $pass);
    }


    function inLog($arr, $con)
    {
        $masterid = $arr->masterid ? $arr->masterid : 0;
        $array = array('intype' => 1, 'createdate' => date('Y-m-d H:i:s'), 'masterid' => '0', 'bmasterid' => $masterid, 'ip' => get_ip(), 'con' => $con);
        db::_insert('log', $array);
    }

    function get_info(){
        $sql='select * from __user where `uid`='.$this->userid;
        return db::get_one($sql);
    }

    static function exitSys()
    {
        unset($_SESSION['k_user']);
        unset($_SESSION['k_key']);
        unset($_SESSION['k_masterid']);
        unset($_SESSION['k_nick']);
        unset($_SESSION['k_roleid']);
        unset($_SESSION['k_rolename']);
        unset($_SESSION['k_right']);
    }

}